package com.baihan.angus.app.game.basic.base;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.baihan.angus.app.game.basic.R;

import java.util.ArrayList;
import java.util.List;

public abstract class BaseAdp<T, VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter {


    private static final int EMPTY_VIEW = 0xaa01;
    private static final int FIRST_LOADING_VIEW = 0xaa02;
    private static final int ITEM_VIEW = 0xaa03;
    protected List<T> mData = new ArrayList<>();
    private RecyclerView mRecyclerView;
    private int emptyViewId = 0;

    public BaseAdp() {
    }

    public BaseAdp(boolean firstLoading) {
        this.isFirstLoading = firstLoading;
    }

    protected RecyclerView getRecyclerView() {
        return mRecyclerView;
    }

    public void setNewData(List<T> data) {
        if (isFirstLoading) isFirstLoading = false;
        mData.clear();
        mData.addAll(data);
        notifyDataSetChanged();
    }

    public void add(@NonNull T t) {
        if (isFirstLoading) isFirstLoading = false;
        mData.add(t);
        notifyDataSetChanged();
    }

    public void addAll(@NonNull List<T> data) {
        if (isFirstLoading) isFirstLoading = false;
        mData.addAll(data);
        notifyDataSetChanged();
    }

    public void clear() {
        mData.clear();
    }

    public T getItem(int position) {
        if (mData.size() == 0) return null;
        return mData.get(position);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == EMPTY_VIEW) {
            return createEmptyViewHolder(parent, viewType);
        } else if (viewType == FIRST_LOADING_VIEW) {
            return createFirstLoadingViewHolder(parent, viewType);
        }
        return createYourViewHolder(parent, viewType);
    }

    private RecyclerView.ViewHolder createFirstLoadingViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(getFirstLoadingView(), parent, false);
        return new FirstLoadingViewHolder(view);
    }

    private RecyclerView.ViewHolder createEmptyViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        if (getEmptyView() == 0) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adp_no_data, parent, false);
        } else {
            view = LayoutInflater.from(parent.getContext()).inflate(getEmptyView(), parent, false);
        }
        return new EmptyViewHolder(view);
    }

    protected int getFirstLoadingView() {
        if (isFirstLoading) {
            throw new NullPointerException("请重写并覆盖本函数，返回你的第一次加载布局");
        }
        return 0;
    }

    protected abstract RecyclerView.ViewHolder createYourViewHolder(@NonNull ViewGroup parent, int viewType);

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == ITEM_VIEW) {
            bindYourViewHolder((VH) holder, position);
        }
    }

    protected abstract void bindYourViewHolder(@NonNull VH holder, int position);

    protected abstract int getEmptyView();

    @Override
    public int getItemViewType(int position) {
        if (isFirstLoading) return FIRST_LOADING_VIEW;
        if (mData.size() == 0) return EMPTY_VIEW;
        return ITEM_VIEW;
    }

    @Override
    public int getItemCount() {
        if (isFirstLoading) return 6;
        if (mData.size() > 0) return mData.size();
        return 1;
    }

    public void bindToRecycleView(RecyclerView recyclerView) {
        this.mRecyclerView = recyclerView;
        getRecyclerView().setAdapter(this);
    }

    private boolean isFirstLoading = false;

    public void setFirstLoading(boolean firstLoading) {
        isFirstLoading = firstLoading;
    }


    public class FirstLoadingViewHolder extends RecyclerView.ViewHolder {
        public FirstLoadingViewHolder(@NonNull View itemView) {
            super(itemView);
        }
    }

    public class EmptyViewHolder extends RecyclerView.ViewHolder {
        public EmptyViewHolder(@NonNull View itemView) {
            super(itemView);
        }
    }
}
